package gw.com.android.ui.me;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import gw.com.android.app.AppMain;
import www.com.library.dialog.ToastPopWindow;
import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2017-10-18.
 */
class AlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_CAMERA = 0;
    private static final int TYPE_IMAGE = 1;

    private Activity activity;
    private boolean showCamera;
    private int itemLength;
    private final int maxPick;
    private OnAlbumClickListener listener;

    private List<String> imageList;
    ArrayList<String> picked = new ArrayList<>();

    AlbumAdapter(Activity activity, boolean showCamera, int columnCount, int maxPick) {
        this.activity = activity;
        this.showCamera = showCamera;
        itemLength = DeviceUtil.instance().getScreenPixelsWidth(activity) / columnCount;
        this.maxPick = maxPick;
    }

    AlbumAdapter addPicked(List<String> list) {
        if (list != null) {
            picked.addAll(list);
        }
        return this;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_CAMERA) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_camera, parent, false);
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.width = params.height = itemLength;
            view.setLayoutParams(params);
            return new CameraHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album, parent, false);
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.width = params.height = itemLength;
            view.setLayoutParams(params);
            return new ImageHolder(view);
        }
    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        if (holder instanceof ImageHolder) {
            ImageHolder mHolder = (ImageHolder) holder;
            mHolder.ivImage.setImageResource(0);
            mHolder.setChecked(false);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        boolean showCover = picked.size() >= maxPick;
        if (position == 0) {
            CameraHolder mHolder = (CameraHolder) holder;
            mHolder.viewCover.setVisibility(showCover ? View.VISIBLE : View.GONE);
            mHolder.bindListener();
            return;
        }
        final String item = getItem(position);
        if (item != null && holder instanceof ImageHolder) {
            ImageHolder mHolder = (ImageHolder) holder;
            Glide.with(activity).load(item).into(mHolder.ivImage);
            boolean checked = picked.contains(item);
            mHolder.viewCover.setVisibility(showCover && !checked ? View.VISIBLE : View.GONE);
            mHolder.setChecked(checked);
            mHolder.bindListener(position, item);
        }
    }

    public String getItem(int position) {
        if (showCamera) {
            position--;
        }
        if (position >= 0 && imageList != null && position < imageList.size()) {
            return imageList.get(position);
        }
        return null;
    }


    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_CAMERA : TYPE_IMAGE;
    }

    @Override
    public int getItemCount() {
        int count = showCamera ? 1 : 0;
        if (imageList != null) {
            return count + imageList.size();
        }
        return count;
    }

    void setDatas(List<String> images) {
        this.imageList = images;
        notifyDataSetChanged();
    }

    ArrayList<String> getPicked() {
        return new ArrayList<>(picked);
    }

    private class ImageHolder extends RecyclerView.ViewHolder {

        final ImageView ivImage;
        final ImageView ivChecked;
        final View viewCover;

        ImageHolder(View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.album_image);
            ivChecked = itemView.findViewById(R.id.album_check);
            viewCover = itemView.findViewById(R.id.album_cover);
        }

        void bindListener(final int position, final String path) {
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int previous = picked.size();
                    if (picked.contains(path)) {
                        picked.remove(path);
                        setChecked(false);
                    } else {
                        if (picked.size() >= maxPick) {
                            String text = AppMain.getAppString(R.string.feedback_over_max);
                            new ToastPopWindow(AppMain.getApp(), String.format(Locale.getDefault(), text, maxPick)).show();
                            return;
                        }
                        picked.add(path);
                        setChecked(true);
                    }
                    if (listener != null) {
                        listener.onImage(AlbumAdapter.this, position, path);
                    }
                    if (previous >= maxPick || picked.size() >= maxPick) {
                        notifyDataSetChanged();
                    }
                }
            });
        }

        void setChecked(boolean checked) {
            ivChecked.setImageResource(checked ? R.mipmap.a_photo_checked : R.mipmap.a_photo_normal);
        }

    }

    private class CameraHolder extends RecyclerView.ViewHolder {
        View viewCover;

        CameraHolder(View itemView) {
            super(itemView);
            viewCover = itemView.findViewById(R.id.album_cover);
        }

        void bindListener() {
            this.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onCamera();
                    }
                }
            });
        }

    }

    void setOnAlbumClickListener(OnAlbumClickListener listener) {
        this.listener = listener;
    }

    interface OnAlbumClickListener {
        void onCamera();

        void onImage(AlbumAdapter adapter, int position, String path);
    }

}
